package com.smt.take.ui.fragment

import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.smt.take.R
import com.smt.take.dagger2.component.DaggerHomeFragmentComponent
import com.smt.take.dagger2.module.HomeFragmentModule
import com.smt.take.model.beans.HomeListBean
import com.smt.take.model.beans.Seller
import com.smt.take.presenter.HomeFragmentPresenter
import com.smt.take.ui.adapter.HomeRvAdapter
import kotlinx.android.synthetic.main.fragment_home.*
import org.jetbrains.anko.toast
import javax.inject.Inject

class HomeFragment : Fragment() {

    // 延迟初始化
    private lateinit var homeRvAdapter: HomeRvAdapter
    private lateinit var rvHome: RecyclerView
    @Inject
    lateinit var presenter: HomeFragmentPresenter
    private val data: MutableList<Seller> = mutableListOf()
    private var sum: Int = 0
    private var distance: Int = 0
    private var alpha = 55

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view = View.inflate(activity, R.layout.fragment_home, null)
        rvHome = view.findViewById(R.id.rv_home)
        rvHome.layoutManager = LinearLayoutManager(activity)
        homeRvAdapter = HomeRvAdapter(context)
        rvHome.adapter = homeRvAdapter
        //presenter = HomeFragmentPresenter(this)
        // TODO:解耦view层和p层，通过dagger2(基于注解的依赖注入)生成HomeFragmentPresenter
        DaggerHomeFragmentComponent.builder().homeFragmentModule(HomeFragmentModule(this)).build()
            .inject(this)
        distance = 220.dp2px()
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
    }

    private fun initData() {
        presenter.getHomeInfo()
    }

    fun Int.dp2px(): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            toFloat(),
            resources.displayMetrics
        ).toInt()

    }

    fun onHomeSuccess(dataList: HomeListBean) {
        context?.toast("成功获取首页数据")

        data.clear()
        dataList.nearbySellerList?.let { data.addAll(it) }
        dataList.otherSellerList?.let { data.addAll(it) }
        homeRvAdapter.setData(data)

        // 有数据可以滚动才可以监听滚动事件
        rvHome.setOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                sum += dy
                if (sum > distance) {
                    alpha = 255
                } else {
                    alpha = sum * 200 / distance
                    alpha += 55
                }
                ll_title_container.setBackgroundColor(Color.argb(alpha, 0x31, 0x90, 0xe8))
            }
        })
    }

    fun onHomeFailed() {
        context?.toast("获取首页数据失败")
    }
}